package cn.dmrliu.edu.publish.controller;

import cn.dmrliu.edu.publish.bean.*;
import cn.dmrliu.edu.publish.service.TrafficChannelStatsService;
import cn.dmrliu.edu.publish.service.TrafficKeywordsService;
import cn.dmrliu.edu.publish.service.TrafficVisitorStatsService;
import cn.dmrliu.edu.publish.service.TrafficVisitorTypeStatsService;
import cn.dmrliu.edu.publish.util.DateUtil;
import cn.dmrliu.edu.publish.util.DoubleUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/edu/realtime/traffic")
public class TrafficController {
    @Autowired
    private TrafficChannelStatsService trafficChannelStatsService;
    @Autowired
    private TrafficVisitorStatsService trafficVisitorStatsService;
    @Autowired
    private TrafficVisitorTypeStatsService trafficVisitorTypeStatsService;
    @Autowired
    private TrafficKeywordsService trafficKeywordsService;

    // 各渠道独立访客数
    @RequestMapping("/uvctbych")
    public String getUvCtByCh(@RequestParam(value = "date", defaultValue = "1") Integer date) {
        if (date == 1) {
            date = DateUtil.now();
        }
        List<TrafficUvCtByCh> list = trafficChannelStatsService.getUvCt(date);

        StringBuilder categories = new StringBuilder("[");
        StringBuilder data = new StringBuilder("[");

        for (int i = 0; i < list.size(); i++) {
            TrafficUvCtByCh element = list.get(i);

            categories.append("\"").append(element.getCh()).append("\"");
            data.append("\"").append(element.getUvCt()).append("\"");

            if (i < list.size() - 1) {
                categories.append(",");
                data.append(",");
            } else {
                categories.append("]");
                data.append("]");
            }
        }

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"categories\": " + categories + ",\n" +
                "    \"series\": [\n" +
                "      {\n" +
                "        \"name\": \"独立访客数\",\n" +
                "        \"data\": " + data + "\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "}";
    }

    // 各渠道会话数
    @RequestMapping("/svctbych")
    public String getSvCtByCh(@RequestParam(value = "date", defaultValue = "1") Integer date) {
        if (date == 1) {
            date = DateUtil.now();
        }
        List<TrafficSvCtByCh> list = trafficChannelStatsService.getSvCt(date);

        StringBuilder categories = new StringBuilder("[");
        StringBuilder data = new StringBuilder("[");

        for (int i = 0; i < list.size(); i++) {
            TrafficSvCtByCh element = list.get(i);

            categories.append("\"").append(element.getCh()).append("\"");
            data.append("\"").append(element.getSvCt()).append("\"");

            if (i < list.size() - 1) {
                categories.append(",");
                data.append(",");
            } else {
                categories.append("]");
                data.append("]");
            }
        }

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"categories\": " + categories + ",\n" +
                "    \"series\": [\n" +
                "      {\n" +
                "        \"name\": \"会话总数\",\n" +
                "        \"data\": " + data + "\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "}";
    }

    // 各渠道会话平均浏览页面数
    @RequestMapping("/pvpersessionbych")
    public String getPvPerSessionByCh(@RequestParam(value = "date", defaultValue = "1") Integer date) {
        if (date == 1) {
            date = DateUtil.now();
        }
        List<TrafficPvPerSessionByCh> list = trafficChannelStatsService.getPvPerSession(date);

        StringBuilder categories = new StringBuilder("[");
        StringBuilder data = new StringBuilder("[");

        for (int i = 0; i < list.size(); i++) {
            TrafficPvPerSessionByCh element = list.get(i);

            categories.append("\"").append(element.getCh()).append("\"");
            data.append("\"").append(DoubleUtil.toTwo(element.getPvPerSession())).append("\"");




            if (i < list.size() - 1) {
                categories.append(",");
                data.append(",");
            } else {
                categories.append("]");
                data.append("]");
            }
        }

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"categories\": " + categories + ",\n" +
                "    \"series\": [\n" +
                "      {\n" +
                "        \"name\": \"会话平均浏览页面数\",\n" +
                "        \"data\": " + data + "\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "}";
    }

    // 各渠道会话平均停留时长
    @RequestMapping("/durpersessionbych")
    public String getDurPerSessionByCh(@RequestParam(value = "date", defaultValue = "1") Integer date) {
        if (date == 1) {
            date = DateUtil.now();
        }
        List<TrafficDurPerSessionByCh> list = trafficChannelStatsService.getDurPerSession(date);

        StringBuilder categories = new StringBuilder("[");
        StringBuilder data = new StringBuilder("[");

        for (int i = 0; i < list.size(); i++) {
            TrafficDurPerSessionByCh element = list.get(i);

            categories.append("\"").append(element.getCh()).append("\"");
            data.append("\"").append(DoubleUtil.toTwo(element.getDurPerSession())).append("\"");

            if (i < list.size() - 1) {
                categories.append(",");
                data.append(",");
            } else {
                categories.append("]");
                data.append("]");
            }
        }

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"categories\": " + categories + ",\n" +
                "    \"series\": [\n" +
                "      {\n" +
                "        \"name\": \"会话平均停留时长\",\n" +
                "        \"data\": " + data + "\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "}";
    }

    // 各渠道跳出率
    @RequestMapping("/ujratebych")
    public String getUjRateByCh(@RequestParam(value = "date", defaultValue = "1") Integer date) {
        if (date == 1) {
            date = DateUtil.now();
        }
        List<TrafficUjRateByCh> list = trafficChannelStatsService.getUjRate(date);

        StringBuilder categories = new StringBuilder("[");
        StringBuilder data = new StringBuilder("[");

        for (int i = 0; i < list.size(); i++) {
            TrafficUjRateByCh element = list.get(i);

            categories.append("\"").append(element.getCh()).append("\"");
            data.append("\"").append(DoubleUtil.toTwo(element.getUjRate())).append("\"");

            if (i < list.size() - 1) {
                categories.append(",");
                data.append(",");
            } else {
                categories.append("]");
                data.append("]");
            }
        }

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"categories\": " + categories + ",\n" +
                "    \"series\": [\n" +
                "      {\n" +
                "        \"name\": \"跳出率\",\n" +
                "        \"data\": " + data + "\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "}";
    }

    // 流量分时统计
    @RequestMapping("/visitorperhr")
    public String getVisitorPerHr(@RequestParam(value = "date", defaultValue = "1") Integer date) {
        if (date == 1) {
            date = DateUtil.now();
        }

        List<TrafficVisitorStatsPerHour> list = trafficVisitorStatsService.getVisitorStatsPerHour(date);
        if (list == null || list.size() == 0) {
            return "";
        }

        TrafficVisitorStatsPerHour[] perHrArr = new TrafficVisitorStatsPerHour[24];
        for (TrafficVisitorStatsPerHour trafficVisitorStatsPerHour : list) {
            Integer hr = trafficVisitorStatsPerHour.getHr();
            perHrArr[hr] = trafficVisitorStatsPerHour;
        }
        String[] hrArr = new String[24];
        Long[] uvCtArr = new Long[24];
        Long[] pvCtArr = new Long[24];
        Long[] newUvCtArr = new Long[24];


        for (int hr = 0; hr < 24; hr++) {
            hrArr[hr] = String.format("%02d", hr);
            TrafficVisitorStatsPerHour trafficVisitorStatsPerHour = perHrArr[hr];
            if (trafficVisitorStatsPerHour != null) {
                uvCtArr[hr] = trafficVisitorStatsPerHour.getUvCt();
                pvCtArr[hr] = trafficVisitorStatsPerHour.getPvCt();
                newUvCtArr[hr] = trafficVisitorStatsPerHour.getNewUvCt();
            } else {
                uvCtArr[hr] = 0L;
                pvCtArr[hr] = 0L;
                newUvCtArr[hr] = 0L;
            }

        }

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"categories\": [\"" + StringUtils.join(hrArr, "\",\"") + "\"],\n" +
                "    \"series\": [\n" +
                "      {\n" +
                "        \"name\": \"独立访客数\",\n" +
                "        \"data\": [\"" + StringUtils.join(uvCtArr, "\",\"") + "\"]\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"页面浏览数\",\n" +
                "        \"data\": [\"" + StringUtils.join(pvCtArr, "\",\"") + "\"]\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"新访客数\",\n" +
                "        \"data\": [\"" + StringUtils.join(newUvCtArr, "\",\"") + "\"]\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "}";

    }

    // 各访客类型的流量统计， 独立访客数，页面浏览数，跳出率，平均在线时长，平均访问页面数
    @RequestMapping("/visitorpertype")
    public String getVisitorPerType(@RequestParam(value = "date", defaultValue = "1") Integer date) {
        if (date == 1) {
            date = DateUtil.now();
        }
        List<TrafficVisitorTypeStats> list = trafficVisitorTypeStatsService.getVisitorPerType(date);

        if (list == null || list.size() == 0) {
            return "";
        }
        TrafficVisitorTypeStats newType = new TrafficVisitorTypeStats(
                "1",
                0L,
                0L,
                0L,
                0L,
                0L
        );
        TrafficVisitorTypeStats oldType = new TrafficVisitorTypeStats("0",
                0L,
                0L,
                0L,
                0L,
                0L
        );

        for (TrafficVisitorTypeStats visitorTypeStats : list) {
            if ("1".equals(visitorTypeStats.getIsNew())) {
                newType = visitorTypeStats;
            } else {
                oldType = visitorTypeStats;
            }
        }

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"total\": 2,\n" +
                "    \"columns\": [\n" +
                "      {\n" +
                "        \"name\": \"访客类型\",\n" +
                "        \"id\": \"isNew\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"独立访客数\",\n" +
                "        \"id\": \"uvCt\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"页面浏览数\",\n" +
                "        \"id\": \"pvCt\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"跳出率\",\n" +
                "        \"id\": \"ujRate\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"平均在线时长\",\n" +
                "        \"id\": \"durPerSession\",\n" +
                "        \"accuracy\": 2\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"平均访问页面数\",\n" +
                "        \"id\": \"pvPerSession\",\n" +
                "        \"accuracy\": 2\n" +
                "      }\n" +
                "    ],\n" +
                "    \"rows\": [  " + newType.toJoinString() + "," + oldType.toJoinString() + " ]\n" +
                "  }\n" +
                "}";
    }

    // 关键词统计
    @RequestMapping("/keywords")
    public String getKeywords(@RequestParam(value = "date", defaultValue = "1") Integer date) {
        if (date == 1) {
            date = DateUtil.now();
        }

        List<TrafficKeywords> list = trafficKeywordsService.getKeywords(date);

        StringBuilder result = new StringBuilder();
        result.append("{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": [");

        for (int i = 0; i < list.size(); i++) {
            TrafficKeywords trafficKeywords = list.get(i);
            result.append(trafficKeywords.toJoinString());
            if (i < list.size() - 1) {
                result.append(",");
            }
        }
        result.append("]\n" +
                "}");

        return result.toString();
    }
}
